package com.yayan.beautyclub.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.yayan.beautyclub.bean.Reserve;
import com.yayan.beautyclub.dao.IReserveDao;
import com.yayan.beautyclub.dao.ReserveDao;

@Service
public class ReserveService implements IReserveService {

	private IReserveDao reserveDao;
	
	public ReserveService(){
		reserveDao = new ReserveDao();
	}
	
	@Override
	public List<Map<String, Object>> findReserveByUserId(long uId, int status, int page, int size) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = null;
		try{
			if(uId != 0L && page >= 0 && size >= 0){
				list = reserveDao.findReserveByUserId(uId, status, page, size);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Map<String, Object> findReserveById(long rId) {
		// TODO Auto-generated method stub
		Map<String, Object> map = null;
		try{
			if(rId != 0L){
				map = reserveDao.findReserveById(rId);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public long saveReserve(Reserve reserve) {
		// TODO Auto-generated method stub
		long r = 0L;
		try{
			if(reserve.getpId() > 0L && !reserve.getName().isEmpty() && !reserve.getPhone().isEmpty()){
				if(reserve.getTime() == null){
					reserve.setTime(new Date());
				}
				if(reserve.getrTime() == null){
					reserve.setrTime(new Date());
				}
				r = reserveDao.save(reserve);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return r;
	}

	@Override
	public boolean cancelReserve(long rId) {
		// TODO Auto-generated method stub
		boolean res = false;
		try{
			if(rId != 0L){
				res = reserveDao.cancel(rId);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return res;
	}

	@Override
	public boolean deleteReserve(long rId) {
		// TODO Auto-generated method stub
		boolean res = false;
		try{
			if(rId != 0L){
				res = reserveDao.delete(rId);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return res;
	}

	@Override
	public List<Map<String, Object>> findReserveByCondition(int page, int size, int firstcId, int secondcId,
			String searchKey) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = null;
		try{
			if(page > 0 && size > 0){
				list = reserveDao.findReserveByCondition(page, size, firstcId, secondcId, searchKey);				
			}
	        
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

}
